package com.hzhk.happyrent.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class KeyboardUtil {

	public static boolean hideSoftInput(Activity acitivity) {
		InputMethodManager imm = (InputMethodManager) acitivity
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		return imm.hideSoftInputFromWindow(acitivity.getWindow().getDecorView()
				.getApplicationWindowToken(), 0); //强制隐藏键盘
//		return imm.hideSoftInputFromWindow(acitivity.getWindow().getDecorView()
//				.getApplicationWindowToken(),
//				InputMethodManager.HIDE_NOT_ALWAYS);
	}

	public static void showSoftInput(EditText et) {
		et.requestFocus();
		InputMethodManager imm = (InputMethodManager) et.getContext()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
//		imm.showSoftInput(et, InputMethodManager.RESULT_UNCHANGED_SHOWN);
		imm.showSoftInput(et, InputMethodManager.SHOW_FORCED);
	}

	public static void showSoftInputDelay(final EditText et) {
		et.postDelayed(new Runnable() {

			@Override
			public void run() {
				showSoftInput(et);
			}
		}, 300);
	}

	/**
	 * 隐藏键盘
	 *
	 * @param edit
	 *            获得焦点的控�?
	 */
	public static void hideKeyboard(Activity activity, EditText edit) {
		InputMethodManager im = (InputMethodManager) edit.getContext()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		im.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);
	}

	/**
	 * 显示键盘
	 *
	 * @param edit
	 *            获得焦点的控�?
	 */
	public static void showKeyboard(EditText edit) {
		edit.setFocusable(true);
		edit.setFocusableInTouchMode(true);
		edit.requestFocus();
		edit.requestFocusFromTouch();
		InputMethodManager im = (InputMethodManager) edit.getContext()
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		im.showSoftInput(edit, InputMethodManager.RESULT_UNCHANGED_SHOWN);
	}

	public static boolean KeyBoardIsHidden(Activity activity) {
		if (activity.getWindow().getAttributes().softInputMode == WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
			return true;
		}
		return false;
	}

	public static boolean KeyBoardIsShowNew(Activity context) {
		if (context == null) {
			return false;
		}
		final View v = ((Activity) context).getWindow().peekDecorView();
		if (v != null && v.getWindowToken() != null) {
			return true;
		}
		return false;
	}
	public static boolean keyBoardHiddenOrShow(EditText et, final Activity context){
		et.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener(){

            //当键盘弹出隐藏的时候会 调用此方法。
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                //获取当前界面可视部分
                context.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //获取屏幕的高度
                int screenHeight = context.getWindow().getDecorView().getRootView().getHeight();
                //此处就是用来获取键盘的高度的， 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
                int heightDifference = screenHeight - r.bottom;
            }
            
        });
		return true;
	}
}
